*******************************************************************************
* .do-file to adjust run the regression analysis on final dataset
* first: adjust final variables for regression analysis
* second: run regression and create graphs
*********************************************************************************

		clear all
		cd "$scfwkddata"
		
		use dataset_analysis, clear
		
*************************************************************
************* prepare for multiple impuation ****************
*************************************************************		

		tostring Y1, replace
		g imp = substr(Y1,-1,.)
		
		tostring wave, replace
		g Y1_uni = wave+Y1
		
		destring Y1 imp wave Y1_uni, replace
		
		
		**** how many observations by marital status? ****
		
		count if single == 0 & imp == 1
		count if single == 1 & imp == 1
		count if couple != . & imp == 1

		
********************************************************
********** adjust variables for regressions ************
********************************************************	

		// ageref: squared and cubic
		g agerefsq  = ageref^2
		replace agerefsq = agerefsq /100
		g agerefcb  = ageref^3
		replace agerefcb = agerefcb /10000
		
		// ageav: squared and cubic
		g ageavsq  = ageav^2
		replace ageavsq = ageavsq /100
		g ageavcb  = ageav^3
		replace ageavcb = ageavcb /10000
		
		// inverse hyperbolic sine for income and wealth
		ihstrans totinc_nocap
		rename ihs_totinc_nocap inc_trans
		
		ihstrans totinc_nocapoth 
		rename ihs_totinc_nocap incoth_trans
		
		ihstrans finassets
		rename ihs_finassets assets_trans
		
		ihstrans safe 
		rename ihs_safe safe_trans
		
		ihstrans safe_nw
		rename ihs_safe_nw safe_nw_trans
		
		ihstrans risky 
		rename ihs_risky risky_trans
		
		// conditional risky share
		g share_an  = share if share > 0 
		g share2_an = share2 if share2 > 0
		
		// education dummy (high vs. low education, referring to more or less than 12 yrs of schooling)
		g educcat   = 0 if educref < 8
		replace educcat = 1 if educref == 8
		
		// number of kids (at home and elsewhere below 25)
		replace numkids25 = . if numkids25 < 0

		// set as survey data
		svyset Y1 [pweight = wgt_rv] 
		
		// variables for confidence bands for life cycle profiles	
		gen ci_low  = . 
		gen ci_high = .

		
		///////////////////////////////////////////////////////////////////////////////
		///////////////////////// LIFE CYCLE  GRAPHS //////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////
		
		****************************************************
		**** STOCK MARKET PARTICIPATION RATE - BASELINE ****
		****************************************************
		
		// computing average by age 
		replace ci_low  = . 
		replace ci_high = . 
		gen mSMP    = . 
		
		foreach x of numlist 0/2 {
		foreach num of numlist 30(1)65 {
		svy: mean SMP if ageav == `num' & fam_type == `x'
		mat A = r(table)  
		replace mSMP    = A[rownumb(A, "b"),1 ] if ageav == `num' & fam_type == `x'  
		replace ci_low  = A[rownumb(A, "ll"),1 ] if ageav == `num' & fam_type == `x' 
		replace ci_high = A[rownumb(A, "ul"),1 ] if ageav == `num' & fam_type == `x' 
		}
		}
		
		preserve
		keep mSMP ageav fam_type ci_low ci_high
		duplicates drop
		sort ageav
		
		tw (line mSMP ageav if fam_type == 2 , lc(black) lw(medium)) ///
		(line ci_low ageav  if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(line ci_high ageav if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(line ci_low ageav  if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(line mSMP ageav    if fam_type == 0, lp(dot) lc(black) lw(medium)) ///
		(line ci_high ageav if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(line ci_high ageav if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(line ci_low ageav  if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(line mSMP ageav    if fam_type == 1, lp(dash)  lc(black) lw(medium)) ,  ///
		xtitle("Age", siz(vlarge)) ytitle("Participation Rate", size(vlarge))  ///
		legend(order(1 "Couples" 5 "Men" 9 "Women") size(vlarge) ///
		position(6) col(4) region(lcolor(white)) stack) ///
		graphregion(color(white)) xlabel(30(10)65,labsize(vlarge)) ylabel(0(.2)0.8,labsize(vlarge))
		graph export "$resultpath/SMP_lifecycle_data.eps", replace
		graph export "$resultpath/SMP_lifecycle_data.pdf", replace
		restore
		
		// export data for comparison to model -- couples 
		preserve
		keep if couple != .
		keep ageav mSMP couple
		bysort ageav couple: gen dup = _n
		keep if dup == 1
		sort couple ageav
		drop dup
		outsheet using "$resultpath/SMP_couple.csv", ///
		comma nonames nolabel replace 
		restore
		
		preserve
		keep if couple != .
		keep ageav ci_low ci_high couple
		bysort ageav couple: gen dup = _n
		keep if dup == 1
		sort couple ageav
		drop dup
		outsheet using "$resultpath/SMP_couple_conf.csv", ///
		comma nonames nolabel replace 
		restore
		
		// export data for comparison to model -- singles
		preserve
		keep if single != .
		keep ageav mSMP single
		bysort ageav single: gen dup = _n
		keep if dup == 1
		sort single ageav
		drop dup
		outsheet using "$resultpath/SMP_single.csv", ///
		comma nonames nolabel replace 
		restore
		
		preserve
		keep if single != .
		keep ageav ci_low ci_high single 
		duplicates drop
		sort single ageav
		outsheet using "$resultpath/SMP_single_conf.csv", ///
		comma nonames nolabel replace 
		restore
		
	
		******************************************************************************
		**** STOCK MARKET PARTICIPATION RATE - TIGHTER DEFINITION OF RISKY ASSETS ****
		******************************************************************************
		
		// computing average by age
		gen mSMP2 = .
		foreach x of numlist 0/2 {
		foreach num of numlist 30(1)65 {
		svy: mean SMP2 if ageav == `num' & fam_type == `x'
		mat A = r(table) 
		replace mSMP2    = A[rownumb(A, "b"),1 ] if ageav == `num' & fam_type == `x' & nw != .
		replace ci_low  = A[rownumb(A, "ll"),1 ] if ageav == `num' & fam_type == `x' & nw != .
		replace ci_high = A[rownumb(A, "ul"),1 ] if ageav == `num' & fam_type == `x' & nw != .
		}
		}
		
		preserve
		keep mSMP2 ageav fam_type ci_low ci_high
		duplicates drop
		sort ageav
		
		tw (lowess mSMP2 ageav if fam_type == 2 , lc(black) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess mSMP2 ageav    if fam_type == 0, lp(dot) lc(black) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess mSMP2 ageav    if fam_type == 1, lp(dash)  lc(black) lw(medium)) ,  ///
		xtitle("Age", siz(vlarge)) ytitle("Participation Rate", size(vlarge))  ///
		legend(order(1 "Couples" 5 "Men" 9 "Women") size(vlarge) ///
		position(6) col(4) region(lcolor(white)) stack) ///
		graphregion(color(white)) xlabel(30(10)65,labsize(vlarge)) ylabel(0(.2)0.6,labsize(vlarge))
		graph export "$resultpath/SMP_lifecycle_data_noretirement.eps", replace
		graph export "$resultpath/SMP_lifecycle_data_noretirement.pdf", replace
		restore
		
		
		
		********************************************************
		**** STOCK MARKET PARTICIPATION RATE - ONE COHORT ******
		********************************************************
		preserve
		keep if byav > 1944 & byav < 1961
		drop mSMP
		
		gen mSMP = .
		replace ci_low = .
		replace ci_high = .
		foreach x of numlist 0/2 {
		foreach num of numlist 30(1)65 {
		svy: mean SMP if ageav == `num' & fam_type == `x'
		mat A = r(table) 
		replace mSMP    = A[rownumb(A, "b"),1 ] if ageav == `num' & fam_type == `x'  & nw != .
		replace ci_low  = A[rownumb(A, "ll"),1 ] if ageav == `num' & fam_type == `x' & nw != .
		replace ci_high = A[rownumb(A, "ul"),1 ] if ageav == `num' & fam_type == `x' & nw != .
		}
		}
		duplicates drop
		tw (lowess mSMP ageav if fam_type == 2 , lc(black) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess mSMP ageav    if fam_type == 0, lp(dot) lc(black) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess mSMP ageav    if fam_type == 1, lp(dash)  lc(black) lw(medium)) ,  ///
		xtitle("Age", siz(vlarge)) ytitle("Participation Rate", size(vlarge))  ///
		legend(order(1 "Couples" 5 "Men" 9 "Women") size(vlarge) ///
		position(6) col(4) region(lcolor(white)) stack) ///
		graphregion(color(white)) xlabel(30(10)65,labsize(vlarge)) ylabel(0(.2)0.6,labsize(vlarge))
		graph export "$resultpath/SMP_lifecycle_data_onecohort.eps", replace
		graph export "$resultpath/SMP_lifecycle_data_onecohort.pdf", replace
		restore
		
		
		************************************************
		****** CONDITIONAL RISKY SHARE - BASELINE ******
		************************************************
		
		// computing average by age
		replace ci_low   = . 
		replace ci_high  = . 
		gen mshare_an    = . 
		
		foreach x of numlist 0/2 {
		foreach num of numlist 30(1)65 {
		svy: mean share_an if ageav == `num' & fam_type == `x'
		mat A = r(table) 
		replace mshare_an   = A[rownumb(A, "b"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_low  = A[rownumb(A, "ll"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_high = A[rownumb(A, "ul"),1 ] if ageav == `num' & fam_type == `x'
		}
		}
		
		preserve
		keep  mshare_an ageav fam_type ci_low ci_high
		duplicates drop
		sort ageav
		
		tw (line mshare_an ageav if fam_type == 2 , lc(black) lw(medium)) ///
		(line ci_low ageav  if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(line ci_high ageav if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(line ci_low ageav  if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(line mshare_an ageav    if fam_type == 0, lp(dot) lc(black) lw(medium)) ///
		(line ci_high ageav if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(line ci_high ageav if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(line ci_low ageav  if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(line mshare_an ageav    if fam_type == 1, lp(dash)  lc(black) lw(medium)) ,  ///
		xtitle("Age", siz(vlarge)) ytitle("Risky Share in %", size(vlarge))  ///
		legend(order(1 "Couples" 5 "Men" 9 "Women") size(vlarge) ///
		position(6) col(4) region(lcolor(white)) stack) ///
		graphregion(color(white)) xlabel(30(10)65,labsize(vlarge)) ylabel(0.1(.2)0.5,labsize(vlarge))
		graph export "$resultpath/share_lifecycle_data.eps", replace
		graph export "$resultpath/share_lifecycle_data.pdf", replace
		restore
		
		
		// export data for comparison to model -- couples
		preserve
		keep if couple != .
		keep ageav mshare_an couple
		bysort ageav couple: gen dup = _n
		keep if dup == 1
		sort couple ageav
		drop dup
		outsheet using "$resultpath/share_an_couple.csv", ///
		comma nonames nolabel replace 
		restore
		
		preserve
		keep if couple != .
		keep ageav ci_low ci_high couple
		bysort ageav couple: gen dup = _n
		keep if dup == 1
		sort couple ageav
		drop dup
		outsheet using "$resultpath/share_an_couple_conf.csv", ///
		comma nonames nolabel replace 
		restore
		
		
		// export data for comparison to model -- singles
		preserve
		keep if single != .
		keep ageav mshare_an single
		bysort ageav single: gen dup = _n
		keep if dup == 1
		sort single ageav
		drop dup
		outsheet using "$resultpath/share_an_single.csv", ///
		comma nonames nolabel replace 
		restore
		
		preserve
		keep if single != .
		keep ageav ci_low ci_high single 
		bysort ageav single: gen dup = _n
		keep if dup == 1
		sort single ageav
		drop dup
		outsheet using "$resultpath/share_an_single_conf.csv", ///
		comma nonames nolabel replace 
		restore
		

		***********************************************************************
		****** CONDITIONAL RISKY SHARE - TIGHTER DEFINITION OF RISKY ASSETS ***
		***********************************************************************
		
		// computing average by age
		gen mshare2_an = .
		foreach x of numlist 0/2 {
		foreach num of numlist 30(1)65 {
		svy: mean share2_an if ageav == `num' & fam_type == `x'
		mat A = r(table) 
		replace mshare2_an   = A[rownumb(A, "b"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_low  = A[rownumb(A, "ll"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_high = A[rownumb(A, "ul"),1 ] if ageav == `num' & fam_type == `x'
		}
		}
		
		preserve
		keep  mshare2_an ageav fam_type ci_low ci_high
		duplicates drop
		sort ageav
		
		tw (lowess mshare2_an ageav if fam_type == 2 , lc(black) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 2 , lc(black*0.15) lw(mediumn)) ///
		(lowess ci_high ageav if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess mshare2_an ageav    if fam_type == 0, lp(dot) lc(black) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess mshare2_an ageav    if fam_type == 1, lp(dash)  lc(black) lw(medium)) ,  ///
		xtitle("Age", siz(vlarge)) ytitle("Risky Share in %", size(vlarge))  ///
		legend(order(1 "Couples" 5 "Men" 9 "Women") size(vlarge) ///
		position(6) col(4) region(lcolor(white)) stack) ///
		graphregion(color(white)) xlabel(30(10)65,labsize(vlarge)) ylabel(0.2(.2)0.4,labsize(vlarge))
		graph export "$resultpath/share_lifecycle_data_noretirement.eps", replace
		graph export "$resultpath/share_lifecycle_data_noretirement.pdf", replace
		restore
		
		
		*************************************************
		****** CONDITIONAL RISKY SHARE - ONE COHORT *****
		*************************************************
		
		preserve
		// computing average by age
		keep if byav > 1944 & byav < 1961
		drop mshare_an
		gen mshare_an = .
		replace ci_low = .
		replace ci_high = .
		foreach x of numlist 0/2 {
		foreach num of numlist 30(1)65 {
		svy: mean share_an if ageav == `num' & fam_type == `x'
		mat A = r(table) 
		replace mshare_an    = A[rownumb(A, "b"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_low  = A[rownumb(A, "ll"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_high = A[rownumb(A, "ul"),1 ] if ageav == `num' & fam_type == `x'
		}
		}
		duplicates drop
		tw (lowess mshare_an ageav if fam_type == 2 , lc(black) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess mshare_an ageav    if fam_type == 0, lp(dot) lc(black) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess mshare_an ageav    if fam_type == 1, lp(dash)  lc(black) lw(medium)) ,  ///
		xtitle("Age", siz(vlarge)) ytitle("Risky Share in %", size(vlarge))  ///
		legend(order(1 "Couples" 5 "Men" 9 "Women") size(vlarge) ///
		position(6) col(4) region(lcolor(white)) stack) ///
		graphregion(color(white)) xlabel(30(10)65,labsize(vlarge)) ylabel(0(.2)0.6,labsize(vlarge))
		graph export "$resultpath/share_lifecycle_data_onecohort.eps", replace
		graph export "$resultpath/share_lifecycle_data_onecohort.pdf", replace
		restore
		
		
		************************************************
		**** UNDCONDITIONAL RISKY SHARE - BASELINE *****
		************************************************
		
		// computing the average
		replace ci_low   = . 
		replace ci_high  = . 
		gen mshare    = . 
		
		foreach x of numlist 0/2 {
		foreach num of numlist 30(1)65 {
		svy: mean share if ageav == `num' & fam_type == `x'
		mat A = r(table) 
		replace mshare   = A[rownumb(A, "b"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_low  = A[rownumb(A, "ll"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_high = A[rownumb(A, "ul"),1 ] if ageav == `num' & fam_type == `x'
		}
		}
		
		preserve
		keep  mshare ageav fam_type ci_low ci_high
		duplicates drop
		sort ageav
		
		tw (line mshare ageav if fam_type == 2 , lc(black) lw(medium)) ///
		(line ci_low ageav  if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(line ci_high ageav if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(line ci_low ageav  if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(line mshare ageav    if fam_type == 0, lp(dot) lc(black) lw(medium)) ///
		(line ci_high ageav if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(line ci_high ageav if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(line ci_low ageav  if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(line mshare ageav    if fam_type == 1, lp(dash)  lc(black) lw(medium)) ,  ///
		xtitle("Age", siz(vlarge)) ytitle("Risky Share in %", size(vlarge))  ///
		legend(order(1 "Couples" 5 "Men" 9 "Women") size(vlarge) ///
		position(6) col(4) region(lcolor(white)) stack) ///
		graphregion(color(white)) xlabel(30(10)65,labsize(vlarge)) ylabel(0(.1)0.3,labsize(vlarge))
		graph export "$resultpath/ucshare_lifecycle_data.eps", replace
		graph export "$resultpath/ucshare_lifecycle_data.pdf", replace
		restore
		
		// export data for comparison to model -- couples
		preserve
		keep if couple != .
		keep ageav mshare couple
		bysort ageav couple: gen dup = _n
		keep if dup == 1
		sort couple ageav
		drop dup
		outsheet using "$resultpath/share_couple.csv", ///
		comma nonames nolabel replace 
		restore
		
		preserve
		keep if couple != .
		keep ageav ci_low ci_high couple
		bysort ageav couple: gen dup = _n
		keep if dup == 1
		sort couple ageav
		drop dup
		outsheet using "$resultpath/share_couple_conf.csv", ///
		comma nonames nolabel replace 
		restore
		
		// export data for comparison to model -- singles
		preserve
		keep if single != .
		keep ageav mshare single
		bysort ageav single: gen dup = _n
		keep if dup == 1
		sort single ageav
		drop dup
		outsheet using "$resultpath/share_single.csv", ///
		comma nonames nolabel replace 
		restore	
		
		preserve
		keep if single != .
		keep ageav ci_low ci_high single 
		bysort ageav single: gen dup = _n
		keep if dup == 1
		sort single ageav
		drop dup
		outsheet using "$resultpath/share_single_conf.csv", ///
		comma nonames nolabel replace 
		restore
				

		***********************************************************************
		****** UNCONDITIONAL RISKY SHARE - TIGHTER DEFINITION OF RISKY ASSETS *
		***********************************************************************
		
		// computing the average
		gen mshare2 = .
		
		foreach x of numlist 0/2 {
		foreach num of numlist 30(1)65 {
		svy: mean share2 if ageav == `num' & fam_type == `x'
		mat A = r(table) 
		replace mshare2   = A[rownumb(A, "b"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_low  = A[rownumb(A, "ll"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_high = A[rownumb(A, "ul"),1 ] if ageav == `num' & fam_type == `x'
		}
		}
		
		preserve
		keep  mshare2 ageav fam_type ci_low ci_high
		duplicates drop
		sort ageav
		
		tw (lowess mshare2 ageav if fam_type == 2 , lc(black) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess mshare2 ageav    if fam_type == 0, lp(dot) lc(black) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess mshare2 ageav    if fam_type == 1, lp(dash)  lc(black) lw(medium)) ,  ///
		xtitle("Age", siz(vlarge)) ytitle("Risky Share in %", size(vlarge))  ///
		legend(order(1 "Couples" 5 "Men" 9 "Women") size(vlarge) ///
		position(6) col(4) region(lcolor(white)) stack) ///
		graphregion(color(white)) xlabel(30(10)65,labsize(vlarge)) ylabel(0(.05)0.1,labsize(vlarge))
		graph export "$resultpath/ucshare_lifecycle_data_noretirement.eps", replace
		graph export "$resultpath/ucshare_lifecycle_data_noretirement.pdf", replace
		restore

		***************************************************
		****** UNCONDITIONAL RISKY SHARE - ONE COHORT *****
		***************************************************
		
		preserve
		// computing the average
		keep if byav > 1944 & byav < 1961
		drop mshare
		gen mshare = .
		replace ci_low = .
		replace ci_high = .
		foreach x of numlist 0/2 {
		foreach num of numlist 30(1)65 {
		svy: mean share if ageav == `num' & fam_type == `x'
		mat A = r(table) 
		replace mshare    = A[rownumb(A, "b"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_low  = A[rownumb(A, "ll"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_high = A[rownumb(A, "ul"),1 ] if ageav == `num' & fam_type == `x'
		}
		}
		duplicates drop
		tw (lowess mshare ageav if fam_type == 2 , lc(black) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess mshare ageav    if fam_type == 0, lp(dot) lc(black) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess ci_high ageav if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess ci_low ageav  if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(lowess mshare ageav    if fam_type == 1, lp(dash)  lc(black) lw(medium)) ,  ///
		xtitle("Age", size(vlarge)) ytitle("Risky Share in %", size(vlarge))  ///
		legend(order(1 "Couples" 5 "Men" 9 "Women") size(vlarge) ///
		position(6) col(4) region(lcolor(white)) stack) ///
		graphregion(color(white)) xlabel(30(10)65,labsize(vlarge)) ylabel(0(.1)0.2,labsize(vlarge))
		graph export "$resultpath/ucshare_lifecycle_data_onecohort.eps", replace
		graph export "$resultpath/ucshare_lifecycle_data_onecohort.pdf", replace
		restore
		
		*************************************
		************ NET WORTH ************** 
		*************************************
		
		// computing the average 
		replace nw = nw / 1000
		gen mnw = .
		foreach x of numlist 0/2 {
		foreach num of numlist 30(1)65 {
		svy: mean nw if ageav == `num' & fam_type == `x'
		mat A = r(table) 
		replace mnw   = A[rownumb(A, "b"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_low  = A[rownumb(A, "ll"),1 ] if ageav == `num' & fam_type == `x'
		replace ci_high = A[rownumb(A, "ul"),1 ] if ageav == `num' & fam_type == `x'
		}
		}
		
		********************** SOME MODEL INPUTS ********************************
		
		// for marriage market: average net worth of singles by education and age
		gen nw_aux = nw/5
		foreach num of numlist 30(1)65 {
			di `num'
		svy: mean nw_aux if ageav == `num' & single == 0 & educcat == 0
		svy: mean nw_aux if ageav == `num' & single == 0 & educcat == 1
		svy: mean nw_aux if ageav == `num' & single == 1 & educcat == 0
		svy: mean nw_aux if ageav == `num' & single == 1 & educcat == 1
		}
	
		
		// initial distribution of net worth by family type
		preserve
		replace nw = nw/5
		keep if ageav == 30 & nw >= 0 // condition on positive values bc borrowing not allowed in model
		bysort fam_type: sum nw [aw = wgt_rv] , det
		restore
		
		***************************************************************************

		preserve
		keep mnw ageav fam_type ci_low ci_high
		duplicates drop
		sort ageav
		
		tw (line mnw ageav if fam_type == 2 , lc(black) lw(medium)) ///
		(line ci_low ageav  if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(line ci_high ageav if fam_type == 2 , lc(black*0.15) lw(medium)) ///
		(line ci_low ageav  if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(line mnw ageav if fam_type == 0, lp(dot) lc(black) lw(medium)) ///
		(line ci_high ageav if fam_type == 0, lp(solid) lc(black*0.15) lw(medium)) ///
		(line ci_high ageav if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(line ci_low ageav  if fam_type == 1, lp(solid) lc(black*0.15) lw(medium)) ///
		(line mnw ageav    if fam_type == 1, lp(dash)  lc(black) lw(medium)) ,  ///
		xtitle("Age", siz(vlarge)) ytitle("Net Worth in (000s) 2007\$", size(vlarge))  ///
		legend(order(1 "Couples" 5 "Men" 9 "Women") size(vlarge) ///
		position(6) col(4) region(lcolor(white)) stack) ///
		graphregion(color(white)) xlabel(30(10)65,labsize(vlarge)) ylabel(0(300)1200,labsize(vlarge))
		graph export "$resultpath/networth_lifecycle_data.eps", replace
		graph export "$resultpath/networth_lifecycle_data.pdf", replace
		restore
		
		// export data for comparison to model -- couples
		preserve
		keep if couple != .
		keep ageav mnw couple
		bysort ageav couple: gen dup = _n
		keep if dup == 1
		sort couple ageav
		drop dup
		outsheet using "$resultpath/mnw_couple.csv", ///
		comma nonames nolabel replace 
		restore
		
		preserve
		keep if couple != .
		keep ageav ci_low ci_high couple
		bysort ageav couple: gen dup = _n
		keep if dup == 1
		sort couple ageav
		drop dup
		outsheet using "$resultpath/mnw_couple_conf.csv", ///
		comma nonames nolabel replace 
		restore

		// export data for comparison to model -- singles
		preserve
		keep if single != .
		keep ageav mnw single
		bysort ageav single: gen dup = _n
		keep if dup == 1
		sort single ageav
		drop dup
		outsheet using "$resultpath/mnw_single.csv", ///
		comma nonames nolabel replace 
		restore
		
		preserve
		keep if single != .
		keep ageav ci_low ci_high single 
		bysort ageav single: gen dup = _n
		keep if dup == 1
		sort single ageav
		drop dup
		outsheet using "$resultpath/mnw_single_conf.csv", ///
		comma nonames nolabel replace 
		restore
		
		
		*******************************************************
		************ SOME MORE SAMPLE STATISTICS ************** 
		*******************************************************
		
		// average difference in networth between single men and single women
		ttest mnw, by(single)
		
		// avearge difference in unconditional equity share between single men and single women
		ttest mshare, by(single)
		
		////////////////////////////////////////////////////////////////////////////////////////
		///////////////////////////// REGRESSION TABLES  ///////////////////////////////////////
		////////////////////////////////////////////////////////////////////////////////////////
		
		
		// define interaction terms
		gen singleage = ageref*single

		
		***********************************************
		********* SINGLES - WITHOUT CONTROLS ********** 
		***********************************************

		// Stock Market Particpation 
		micombine reg SMP single singleage ageav ageavsq ageavcb ///
									[iw=wgt_rv] if single != . & nw != . , obsid(Y1_uni) impid(imp)
		
		*marginal effects 
		quietly summarize ageav if e(sample)
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei' 
		nlcom _b[single]+ _b[singleage]* 30
		nlcom _b[single]+ _b[singleage]* 65 
		
		outreg2 using "$resultpath/SMP_table.tex", ///
		ctitle("No controls") addtext(Year FE,No)  ///
		keep(single singleage ageav ageavsq ageavcb) replace
		
		
	
		// Unconditional Share 
		micombine tobit share single singleage ageav ageavsq ageavcb ///
									[iw=wgt_rv] if single != . , ll(0) obsid(Y1_uni) impid(imp) 
		
		*marginal effects
		quietly summarize ageav if e(sample)
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei' 
		nlcom _b[single]+ _b[singleage]* 30
		nlcom _b[single]+ _b[singleage]* 65 

		outreg2 using "$resultpath/equity_table.tex", ///
		ctitle("No controls") addtext(Year FE,No)  ///
		keep(single singleage ageav ageavsq ageavcb) replace


		// Unconditional share as OLS 
		micombine reg share single singleage ageav ageavsq ageavcb ///
									[iw=wgt_rv] if single != . , obsid(Y1_uni) impid(imp) 
		
		*marginal effects
		quietly summarize ageav if e(sample)
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei' 
		nlcom _b[single]+ _b[singleage]* 30
		nlcom _b[single]+ _b[singleage]* 65 

		
		// Conditional Share 
		micombine reg share_an single singleage ageav ageavsq ageavcb  ///
									[iw=wgt_rv] if single != ., obsid(Y1_uni) impid(imp)  
		
		*marginal effects
		quietly summarize ageav if e(sample)
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei' 
		nlcom _b[single]+ _b[singleage]* 30
		nlcom _b[single]+ _b[singleage]* 65 
		
		outreg2 using "$resultpath/riskyshare_table.tex", ///
		ctitle("No controls") addtext(Year FE,No)  ///
		keep(single singleage ageav ageavsq ageavcb) replace

		
		***********************************************
		********* SINGLES - ADDING BASE CONTROLS ******
		***********************************************
		
		// Stock Market Particpation 
		micombine reg SMP single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans i.wave ///
									[iw=wgt_rv] if single != ., obsid(Y1_uni) impid(imp) 
		
		*marginal effects 
		quietly summarize ageav if e(sample)
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei' 
		nlcom _b[single]+ _b[singleage]* 30
		nlcom _b[single]+ _b[singleage]* 65 

		outreg2 using "$resultpath/SMP_table.tex", ///
		ctitle("Base controls") addtext(Year FE,Yes)  ///
		keep(single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans) append

		
		// Unconditional Share 
		micombine tobit share single singleage ageav ageavsq ageavcb i.educcat hh_num  incoth_trans  i.wave ///
									[iw=wgt_rv] if single != . , ll(0) obsid(Y1_uni) impid(imp) 
		
		*marginal effects 
		quietly summarize inc_trans if e(sample)
		local meaninc = r(mean)
		quietly summarize ageav if e(sample)
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei'
		nlcom _b[single]+ _b[singleage]* 30 
		nlcom _b[single]+ _b[singleage]* 65 

		outreg2 using "$resultpath/equity_table.tex", ///
		ctitle("Base controls") addtext(Year FE,Yes)  ///
		keep(single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans) append

	
		// Unconditional share as OLS 
		micombine reg share single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans i.wave ///
									[iw=wgt_rv] if single != . , obsid(Y1_uni) impid(imp) 

		*marginal effects 
		quietly summarize inc_trans if e(sample)
		local meaninc = r(mean)
		quietly summarize ageav if e(sample)
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei'
		nlcom _b[single]+ _b[singleage]* 30 
		nlcom _b[single]+ _b[singleage]* 65 
		
		// Conditional Share 
		micombine reg share_an single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans i.wave ///
									[iw=wgt_rv] if single != ., obsid(Y1_uni) impid(imp) 
		
		*marginal effects 
		quietly summarize ageav if e(sample)
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei' 
		nlcom _b[single]+ _b[singleage]* 30
		nlcom _b[single]+ _b[singleage]* 65 

		outreg2 using "$resultpath/riskyshare_table.tex", ///
		ctitle("Base controls") addtext(Year FE,Yes)  ///
		keep(single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans) append

		
		**********************************************************
		********* SINGLES - ADDING BASE CONTROLS + FIN ASSETS ****
		**********************************************************
	
		// Stock Market Particpation 
		micombine reg SMP single ageav singleage ageavsq ageavcb i.educcat hh_num incoth_trans i.wave safe_nw_trans ///
									[iw=wgt_rv] if single != ., obsid(Y1_uni) impid(imp) 
		
		*marginal effects 
		quietly summarize ageav if e(sample)
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei' 
		nlcom _b[single]+ _b[singleage]* 30 
		nlcom _b[single]+ _b[singleage]* 65 

		outreg2 using "$resultpath/SMP_table.tex", ///
		ctitle("Fin. controls") addtext(Year FE,Yes)  ///
		keep(single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans safe_nw_trans) append

		
		// Unconditional share
		micombine tobit share single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans i.wave safe_nw_trans ///
									[iw=wgt_rv] if single != . , ll(0) obsid(Y1_uni) impid(imp) 
		
		*marginal effects 
		quietly summarize inc_trans if e(sample)
		local meaninc = r(mean)
		quietly summarize ageav if e(sample)
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei'
		nlcom _b[single]+ _b[singleage]* 30 
		nlcom _b[single]+ _b[singleage]* 65 

		outreg2 using "$resultpath/equity_table.tex", ///
		ctitle("Fin. controls") addtext(Year FE,Yes)  ///
		keep(single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans safe_nw_trans) append

		
		// Conditional Share 
		micombine reg share_an single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans i.wave safe_nw_trans ///
									[iw=wgt_rv] if single != ., obsid(Y1_uni) impid(imp) 
		
		*marginal effects 
		quietly summarize ageav if e(sample)
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei' 
		nlcom _b[single]+ _b[singleage]* 30 
		nlcom _b[single]+ _b[singleage]* 65 
		
		outreg2 using "$resultpath/riskyshare_table.tex", ///
		ctitle("Fin. controls") addtext(Year FE,Yes)  ///
		keep(single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans safe_nw_trans) append

	
		
		********************************************************************************
		********* SINGLES - ADDING BASE CONTROLS + FIN ASSETS + OCC AND INDUSTRY FE ****
		********************************************************************************
		
		// code all without information (e.g., not working for pay) as missing
		replace occup_ref = . if occup_ref == 0 
		replace indus_ref = . if indus_ref == 0
		
		
		// Stock Market Particpation 
		micombine reg SMP single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans i.wave i.occup_ref i.indus_ref safe_nw_trans ///
									[iw=wgt_rv] if single != ., obsid(Y1_uni) impid(imp)
		
		*marginal effects 
		quietly summarize ageav if e(sample)
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei'
		nlcom _b[single]+ _b[singleage]* 30
		nlcom _b[single]+ _b[singleage]* 65 

		outreg2 using "$resultpath/SMP_table.tex", ///
		ctitle("Occ. controls") addtext(Year FE,Yes)  ///
		keep(single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans safe_nw_trans) append

	
		// Unconditional share
		micombine tobit share single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans i.wave safe_nw_trans i.occup_ref i.indus_ref ///
									[iw=wgt_rv] if single != ., ll(0) obsid(Y1_uni) impid(imp) 
		
		
		*marginal effects 
		quietly summarize ageav if e(sample) 
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei' 
		nlcom _b[single]+ _b[singleage]* 30 
		nlcom _b[single]+ _b[singleage]* 65 

		outreg2 using "$resultpath/equity_table.tex", ///
		ctitle("Occ. controls") addtext(Year FE,Yes)  ///
			keep(single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans safe_nw_trans) append 

		
		
		// Conditional share
		micombine reg share_an single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans i.wave safe_nw_trans i.occup_ref i.indus_ref ///
									[iw=wgt_rv] if single != ., obsid(Y1_uni) impid(imp) 
		
		*marginal effects 
		quietly summarize ageav if e(sample)
		local meanwei = r(mean)
        nlcom _b[single]+ _b[singleage]* `meanwei'
		nlcom _b[single]+ _b[singleage]* 30
		nlcom _b[single]+ _b[singleage]* 65 

		outreg2 using "$resultpath/riskyshare_table.tex", ///
		ctitle("Occ. controls") addtext(Year FE,Yes)  ///
			keep(single singleage ageav ageavsq ageavcb i.educcat hh_num incoth_trans safe_nw_trans) append

	